package com.software.levelupandroid.mine.viewModel;

import android.content.Context;
import android.os.Looper;
import android.util.Log;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.software.levelupandroid.entity.Badge;
import com.software.levelupandroid.entity.Grade;
import com.software.levelupandroid.entity.MergedBadge;
import com.software.levelupandroid.entity.Resource;
import com.software.levelupandroid.entity.Task;
import com.software.levelupandroid.home.CallBack;
import com.software.levelupandroid.mine.model.MineRepository;

import java.util.List;

/**
 * @Description 个人中心的ViewModel层
 * @Author LiangYaJie
 * @CreateTime 2024年06月11日 10:52:53
 */
public class MineViewModel extends ViewModel {
    private final MutableLiveData<Resource<List<Grade>>> gradeListLiveData;
    private final MutableLiveData<Resource<List<Badge>>> badgeListLiveData;
    private final MutableLiveData<Resource<List<Badge>>> ownBadgeListLiveData;
    private final MutableLiveData<Resource<MergedBadge>> mergedBadgeListLiveData;

    public MineViewModel(){
        gradeListLiveData = new MutableLiveData<>();
        badgeListLiveData = new MutableLiveData<>();
        ownBadgeListLiveData = new MutableLiveData<>();
        mergedBadgeListLiveData = new MutableLiveData<>();
    }

    public MutableLiveData<Resource<List<Grade>>> getGradeListLiveData() {
        return gradeListLiveData;
    }

    public MutableLiveData<Resource<List<Badge>>> getBadgeListLiveData() {
        return badgeListLiveData;
    }

    public MutableLiveData<Resource<List<Badge>>> getOwnBadgeListLiveData() {
        return ownBadgeListLiveData;
    }

    public MutableLiveData<Resource<MergedBadge>> getMergedBadgeListLiveData() {
        return mergedBadgeListLiveData;
    }

    public void getAllGrade(Context context){
        MineRepository.getMineRepository().getAllGrade(new CallBack<List<Grade>>() {
            @Override
            public void onSuccess(List<Grade> data) {
                runOnMainThread(gradeListLiveData, Resource.success(data));
            }

            @Override
            public void onFailure(String msg) {
                Log.e("mine:", msg);
                runOnMainThread(gradeListLiveData, Resource.failure(msg, null));
            }
        }, context);
    }

    public void getAllBadge(Context context){
        MineRepository.getMineRepository().getAllBadge(new CallBack<List<Badge>>() {
            @Override
            public void onSuccess(List<Badge> data) {
                badgeRunOnMainThread(badgeListLiveData, Resource.success(data));
            }

            @Override
            public void onFailure(String msg) {
                Log.e("mine:", msg);
                badgeRunOnMainThread(badgeListLiveData, Resource.failure(msg, null));
            }
        }, context);
    }

    public void getBadgeByUserId(Context context){
        MineRepository.getMineRepository().getBadgeByUserId(new CallBack<List<Badge>>() {
            @Override
            public void onSuccess(List<Badge> data) {
                badgeRunOnMainThread(ownBadgeListLiveData, Resource.success(data));
            }

            @Override
            public void onFailure(String msg) {
                Log.e("mine:", msg);
                badgeRunOnMainThread(ownBadgeListLiveData, Resource.failure(msg, null));
            }
        }, context);
    }

    public void getMergedBadge(Context context){

        Resource<List<Badge>> value = badgeListLiveData.getValue();
        Resource<List<Badge>> value1 = ownBadgeListLiveData.getValue();
        List<Badge> badgeList = value.getData();
        List<Badge> ownBadgeList = value1.getData();

        MergedBadge mergedBadge = new MergedBadge();
        mergedBadge.setBadgeList(badgeList);
        mergedBadge.setOwnBadgeList(ownBadgeList);

        mergedBadgeRunOnMainThread(mergedBadgeListLiveData, Resource.success(mergedBadge));

    }


    /**
     * 将LiveData的赋值放在主线程上
     * @param gradeListLiveData
     * @param data
     */
    private void runOnMainThread(MutableLiveData<Resource<List<Grade>>> gradeListLiveData, Resource<List<Grade>> data) {
        if (Looper.myLooper() == Looper.getMainLooper())
            //当前线程在主线程
            gradeListLiveData.setValue(data);
        else
            //当前线程不在主线程
            gradeListLiveData.postValue(data);
    }

    private void badgeRunOnMainThread(MutableLiveData<Resource<List<Badge>>> gradeListLiveData, Resource<List<Badge>> data) {
        if (Looper.myLooper() == Looper.getMainLooper())
            //当前线程在主线程
            gradeListLiveData.setValue(data);
        else
            //当前线程不在主线程
            gradeListLiveData.postValue(data);
    }

    private void mergedBadgeRunOnMainThread(MutableLiveData<Resource<MergedBadge>> gradeListLiveData, Resource<MergedBadge> data) {
        if (Looper.myLooper() == Looper.getMainLooper())
            //当前线程在主线程
            gradeListLiveData.setValue(data);
        else
            //当前线程不在主线程
            gradeListLiveData.postValue(data);
    }
}
